﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraBars;
using log4net;
using MMS.DAO;
using MMS.Validation;
using DevExpress.XtraEditors;

namespace MMS
{
    public partial class FormUserInfo : Form
    {
        private UserDTO user;
        public bool istxtPasswordActive = false;
        public bool istxtNameActive = false;
        public bool istxtEmailActive = false;
        public bool istxtPhoneActive = false;
        public bool istxtUserNameActive = false;
        private bool UIHasError = false;

        private void FormUserInfo_Load(object sender, EventArgs e)
        {

        }

        public FormUserInfo()
        {
            InitializeComponent();
            this.Text = Utils.APP_TITLE;
            user = new UserDTO();
            user.Id = "";
            dateTimeBirthday.EditValue = DateTime.Today;

        }

        public FormUserInfo(UserDTO userDTO)
        {
            try
            {
                InitializeComponent();
                this.Text = Utils.APP_TITLE;
                this.user = userDTO;
                populateUserInfo();
                bbiSave.Visibility = BarItemVisibility.Never;
                txtName.Focus();
            }
            catch (Exception e)
            {
                LogUtil.log.Error(e);
            }

        }

        private void populateUserInfo()
        {
            txtId.Text = this.user.Id;
            cbActive.Checked = user.Active;
            txtName.Text = user.Name;

            dateTimeBirthday.EditValue = user.Birthday;
            txtEmail.Text = user.Email;
            txtPhone.Text = user.Phone;

            txtAddress.Text = user.Address;

            txtUserName.Text = user.UserName;
            
            // txtPassword.Text = user.Password;
            //get picture
            // picture.Image = user.Picture;

        }

        private void saveUser()
        {
            //unit.Id = Int32.Parse(txtUnitId.Text);
            user.Active = cbActive.Checked;
            user.Name = txtName.Text;
            user.Birthday = DateTime.Parse(dateTimeBirthday.Text);

            user.Email = txtEmail.Text;
            user.Phone = txtPhone.Text;

            user.Address = txtAddress.Text;

            user.UserName = txtUserName.Text;
            string strPassword = txtPassword.Text.Trim();
            if (strPassword.Equals(MessageBundle.BLANK.Value))
            {
                //Password giu nguyen nhu cu
            }
            else
            {
                user.Password = Utils.Md5(strPassword);
            }

            //user.Picture = picture.Image ;

            UserDAO userDAO = new UserDAO(Param.ConnectionString.Value);
            if (user.Id == "")
            {

                userDAO.Insert(user);
            }
            else
            {
                userDAO.Update(user);
            }
        }

        private void clearAll()
        {
            txtId.Text = "";
            txtName.Text = "";
            cbActive.Checked = false;
            txtEmail.Text = "";
            txtAddress.Text = "";
            txtUserName.Text = "";
            txtPhone.Text = "";
            txtPassword.Text = "";
            txtRepeatPassword.Text = "";
        }




        private void txtName_TextChanged(object sender, EventArgs e)
        {
            if (istxtNameActive)
            {
                txtName.BackColor = Color.White;
                istxtNameActive = false;
            }
        }
        private void txtEmail_TextChanged(object sender, EventArgs e)
        {
            if (istxtEmailActive)
            {
                txtEmail.BackColor = Color.White;
                istxtEmailActive = false;
            }
        }
        private void txtPhone_TextChanged(object sender, EventArgs e)
        {
            if (istxtPhoneActive)
            {
                txtPhone.BackColor = Color.White;
                istxtPhoneActive = false;
            }
        }
        private void txtUserName_TextChanged(object sender, EventArgs e)
        {
            if (istxtUserNameActive)
            {
                txtUserName.BackColor = Color.White;
                istxtUserNameActive = false;
            }
        }
        private void Validating_FormUserInfo(object sender, CancelEventArgs CancelE)
        {

            ValidateFormUserInfo.ValidatingFormUserInfo(sender, CancelE, this, dxErrorProviderFormUserInfo);
        }

        private void InvalidValue_FormUserInfo(object sender, DevExpress.XtraEditors.Controls.InvalidValueExceptionEventArgs InvalidEx)
        {
            ValidateFormUserInfo.InvalidValue_FormUserInfo(sender, InvalidEx, this, dxErrorProviderFormUserInfo);
        }

        private void Validateing_KeyPress(object sender, KeyPressEventArgs keyPress)
        {
            ValidateFormUserInfo.ValidatingKeyPressFormUserInfo(sender, keyPress, this);
        }

        private void txtEmail_Validated(object sender, EventArgs e)
        {

        }

        private void txtPasswordAndRepeatPassword_TextChanged(object sender, EventArgs e)
        {
            lbErrorString.Text = "";
            string controlName = ((TextEdit)sender).Name;

            string password = txtPassword.Text.Trim();
            string repeatPassword = txtRepeatPassword.Text.Trim();
            if (!repeatPassword.Equals(password))
            {
                lbErrorString.Text = MessageBundle.ERROR_DIFFERENT_PASSWORD.Value;
                lbErrorString.ForeColor = Color.Red;
                UIHasError = true;
            }
            else
            {
                UIHasError = false;
            }

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            timer1.Stop();
            lbErrorString.Text = "";
        }

        private void barButtonItem_ItemClick(object sender, ItemClickEventArgs e)
        {
            string senderName = e.Item.Name;
            if (!UIHasError && !ValidateFormUserInfo.isEmpty(this))
            {
                if (senderName.Equals(bbiSave.Name))
                {
                    saveUser();
                    clearAll();
                    txtName.Focus();
                }
                else if (senderName.Equals(bbiSaveAndExit.Name))
                {
                    saveUser();
                    this.Close();
                }
            }
            else
            {
                lbErrorString.ForeColor = Color.Red;
                lbErrorString.Text = MessageBundle.ERROR_UI.Value;
                Utils.startTimer(timer1);
            }
            if (senderName.Equals(bbiExit.Name))
            {
                this.Close();
            }
        }
    }



}
